﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Controls_customPager : System.Web.UI.UserControl
{
    private int FirstPazingIndex
    {
        get
        {
            if (ViewState["_GS_FirstPazingIndex"] == null)
                ViewState["_GS_FirstPazingIndex"] = 1;
            return Convert.ToInt32(ViewState["_GS_FirstPazingIndex"]);
        }
        set
        {
            ViewState["_GS_FirstPazingIndex"] = value;
        }
    }

    private int LastPazingIndex
    {
        get
        {
            if (ViewState["_GS_LastPazingIndex"] == null)
                ViewState["_GS_LastPazingIndex"] = MaxPgageToDisplay;
            return Convert.ToInt32(ViewState["_GS_LastPazingIndex"]);
        }
        set
        {
            ViewState["_GS_LastPazingIndex"] = value;
        }
    }
    public int PageSize
    {
        get
        {
            if (ViewState["_GS_PAGESIZE"] == null)
                ViewState["_GS_PAGESIZE"] = 2;
            return Convert.ToInt32(ViewState["_GS_PAGESIZE"]);
        }
        set
        {
            ViewState["_GS_PAGESIZE"] = value;
        }
    }

    public int MaxPgageToDisplay
    {
        get
        {
            if (ViewState["_GS_MaxPgageToDisplay"] == null)
                ViewState["_GS_MaxPgageToDisplay"] = 3;
            return Convert.ToInt32(ViewState["_GS_MaxPgageToDisplay"]);
        }
        set
        {
            ViewState["_GS_MaxPgageToDisplay"] = value;
        }
    }

    public string SortOrder
    {
        get
        {
            if (ViewState["_GS_SORTORDER"] == null)
                return string.Empty;
            else
                return (string)ViewState["_GS_SORTORDER"];
        }
        set
        {
            ViewState["_GS_SORTORDER"] = value;
        }
    }

    public int TotalItems
    {
        get
        {
            if (ViewState["_GS_TOTALITEMS"] == null)
                ViewState["_GS_TOTALITEMS"] = 0;
            return (int)ViewState["_GS_TOTALITEMS"];

        }
        set
        {
            ViewState["_GS_TOTALITEMS"] = value;
        }
    }
  
    private int _totalPages;
    private int TotalPages
    {
        get
        {
            decimal result = (Convert.ToDecimal(TotalItems) / Convert.ToDecimal(PageSize));
            _totalPages = Convert.ToInt32(Math.Ceiling(result));

            return _totalPages;
        }
    }

    public int CurrentPage
    {
        get
        {
            if (ViewState["_GS_CURRENTPAGE"]== null)
                return 1;
            else
                return Convert.ToInt32(ViewState["_GS_CURRENTPAGE"]);
        }

        set
        {
            this.ViewState["_GS_CURRENTPAGE"] = value;
        }
    }

    public bool IsFirstPage
    {
        get
        {
            return (CurrentPage == 1);
        }
    }
    
    public bool IsLastPage
    {
        get
        {
            return (CurrentPage == TotalPages);
        }
    }

    private ArrayList GetSource()
    {
        ArrayList pageSource = new ArrayList();
        if (TotalPages != 0)
        {
            if (TotalPages <= MaxPgageToDisplay)
            {
                FirstPazingIndex =1;
                for (int index = 1; index <= TotalPages; index++)
                {
                    pageSource.Add(index);
                }
                LastPazingIndex = TotalPages;
            }
            else
            {
                
                if (CurrentPage <(LastPazingIndex-1) && CurrentPage>=FirstPazingIndex)
                {
                    FirstPazingIndex = 1;

                    for (int index = 1; index <= MaxPgageToDisplay; index++)
                    {
                        pageSource.Add(index);
                    }

                    LastPazingIndex = MaxPgageToDisplay;
                    pageSource.Add("..");
                    pageSource.Add(TotalPages);

                }
                else if(CurrentPage>=(LastPazingIndex-1))
                {
                    FirstPazingIndex = CurrentPage-1;

                   

                    if (FirstPazingIndex + MaxPgageToDisplay >= TotalPages)
                    {
                        for (int index = FirstPazingIndex; index <=TotalPages; index++)
                        {
                            pageSource.Add(index);
                        }

                        LastPazingIndex = TotalPages;
                    }
                    else
                    {
                        for (int index = 1; index <= MaxPgageToDisplay; index++)
                        {
                            pageSource.Add(FirstPazingIndex + index);
                        }

                        LastPazingIndex = FirstPazingIndex + MaxPgageToDisplay;
                        pageSource.Add("..");
                        pageSource.Add(TotalPages);
                    }
                }
                else
                {
                    FirstPazingIndex = 1;

                    for (int index = 1; index <= MaxPgageToDisplay; index++)
                    {
                        pageSource.Add(index);
                    }

                    LastPazingIndex = MaxPgageToDisplay;
                }

               

            }
        }

        return pageSource;
    }


    public event CommandEventHandler PageIndexChanged;

    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    public void LoadPager()
    {
        if (TotalPages != 0)
        {
            //repCustomPager.DataSource = GetSource();
            //repCustomPager.DataBind();

            lblPagerCount.Text = "Page " + CurrentPage.ToString() + " of " + TotalPages.ToString();
            lnkPrevious.CommandArgument = (CurrentPage - 1).ToString();
            lnkPrevious.Visible = !IsFirstPage;
            lnkNext.CommandArgument = (CurrentPage + 1).ToString();
            lnkNext.Visible = !IsLastPage;
        }
    }

    protected void Pager_DataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            if (e.Item.DataItem.ToString() == CurrentPage.ToString())
            {
                LinkButton lkbPageIndex = (LinkButton)e.Item.FindControl("lkbPageIndex");
                lkbPageIndex.CssClass = "page-index-selected";
                
            }
            else if (e.Item.DataItem.ToString() == "..")
            {
                LinkButton lkbPageIndex = (LinkButton)e.Item.FindControl("lkbPageIndex");
                lkbPageIndex.CommandArgument = LastPazingIndex.ToString();

            }
        }
    }

    
    protected void lkbPageIndex_Command(object sender, CommandEventArgs e)
    {
        if (PageIndexChanged == null)
        {
            throw new Exception("Page index changed event is not implemented");
        }
        CurrentPage = Convert.ToInt32(e.CommandArgument);
        PageIndexChanged(sender, e);
    }

}
